package cn.ccsu.cecs.oos.controller;


import cn.ccsu.cecs.common.oos.Oos;
import cn.ccsu.cecs.common.oos.image.SaveImage;
import cn.ccsu.cecs.common.response.ResponseResult;
import cn.ccsu.cecs.common.utils.PageUtils;
import cn.ccsu.cecs.common.utils.R;
import cn.ccsu.cecs.oos.entity.OosImages;
import cn.ccsu.cecs.oos.service.IOosImagesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 图片资源表 前端控制器
 * </p>
 *
 * @author ccsu-cecs
 * @since 2022-03-05
 */
@Slf4j
@RestController
@RequestMapping("/oos/oos-images")
public class OosImagesController {


    private final SaveImage saveImage;
    @Autowired
    private IOosImagesService oosImagesService;

    public OosImagesController(SaveImage saveImage) {
        this.saveImage = saveImage;
    }

    @ResponseBody
    @PostMapping("test_upload")
    public ResponseResult uploadFile(@RequestParam("image") MultipartFile file) {
        Oos oos = saveImage.checkout(file);
        log.info("{}", oos);
        ResponseResult result = new ResponseResult();
        result.status = saveImage.saveOos(file, oos);
        result.data = new HashMap<String, Object>() {{
            put("oos", oos);
        }};
        return result;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = oosImagesService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        OosImages oosImages = oosImagesService.getById(id);

        return R.ok().put("oosImages", oosImages);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OosImages oosImages) {
        oosImagesService.save(oosImages);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OosImages oosImages) {
        oosImagesService.updateById(oosImages);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        oosImagesService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
}
